package com.fengmi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.CmsContentCat;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.mapper.ContentMapper;
import com.fengmi.service.ContentService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private ContentMapper contentMapper;


    @Override
    public PageResultVO<CmsContentCat> findContentCategoryByPage(PageDTO<CmsContentCat> pageDTO) {
        //非空判断
        if (pageDTO==null){
            return new PageResultVO<>(false,"参数不合法");
        }

        //当前页
        Integer pageNum = pageDTO.getPageNum();
        //每页记录数
        Integer pageSize = pageDTO.getPageSize();
        //进行非空判断
        if (pageNum == null || pageNum<=0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize<=0){
            pageSize = 5;
        }
        //开始分页
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper层查询
        List<CmsContentCat> allContentCategory = contentMapper.findAllContentCategory();
        //分页信息对象创建，放入查询集合
        PageInfo<CmsContentCat> pageInfo = new PageInfo<>(allContentCategory);
        //创建返回对象
        PageResultVO<CmsContentCat> pageResultVO = new PageResultVO<>(true,"查询成功");
        //设置每页数据
        pageResultVO.setData(pageInfo.getList());
        //设置总页数
        pageResultVO.setTotalPage(pageInfo.getPages());
        //设置总记录数
        pageResultVO.setTotalSize(pageInfo.getTotal());

        return pageResultVO;
    }

    @Override
    public ResultVO updateContent(CmsContentCat cmsContentCat) {
        if (cmsContentCat == null){
            return new ResultVO(false,"参数不合法");
        }
        int i = contentMapper.updateContentCategoryById(cmsContentCat);

        return new ResultVO(i==1?true:false,i==1?"修改内容分类信息成功":"修改内容分类信息失败");
    }
}
